package com.ycsj.goldmedalnewconcept.fragment;

import java.util.Timer;

import com.google.gson.Gson;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.bean.ClassSecondResp;
import com.ycsj.goldmedalnewconcept.constant.ConfigConstant;
import com.ycsj.goldmedalnewconcept.utils.SPUtils;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.CycleInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 跟读的Fragment
 * 
 * @author Administrator
 *
 */
public class FollowReadFragment extends BaseFragment implements OnClickListener {
	private static final String TAG = "GrammarFragment";
	private Activity mActivity;
	private TextView mMsgTv;
	private String mMsgName;
	private ImageView iv_home01;
	private ImageView iv_home02;
	private ImageView iv_home03;
	private ImageView iv_home04;
	private Button bt_homework;
	private Timer mTimer;

	public void setMsgName(String msgName) {
		this.mMsgName = msgName;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		this.mActivity = activity;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_follow_read, container,
				false);
		return view; 
	} 

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		initViews(view);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		initDisplay();

	}

	private void initViews(View view) {
		iv_home01 = (ImageView) view.findViewById(R.id.iv_home01);
		iv_home02 = (ImageView) view.findViewById(R.id.iv_home02);
		iv_home03 = (ImageView) view.findViewById(R.id.iv_home03);
		iv_home04 = (ImageView) view.findViewById(R.id.iv_home04);
	}

	private void initDisplay() {
		iv_home01.setOnClickListener(this);
		iv_home02.setOnClickListener(this);
		iv_home03.setOnClickListener(this);
		iv_home04.setOnClickListener(this);
	}

	@Override
	public String getFragmentName() {
		return TAG;
	}

	public OnTouchListener onTouch = new OnTouchListener() {

		@Override
		public boolean onTouch(View v, MotionEvent event) {

			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:

				break;
			case MotionEvent.ACTION_UP:

				Animation shakeAnim = AnimationUtils.loadAnimation(
						getActivity(), R.anim.shake);
				v.startAnimation(shakeAnim);

				break;
			}
			return true;
		}

	};

	public Animation shakeAnimation(int CycleTimes) {
		Animation translateAnimation = new TranslateAnimation(0, 0, -10, 10);
		translateAnimation.setInterpolator(new CycleInterpolator(CycleTimes));
		translateAnimation.setDuration(1000);
		return translateAnimation;
	}

	/**
	 * 点击事件
	 */
	@Override
	public void onClick(View v) {

		//更新SP保存的主页面的ViewPager的当前显示页面数
				SPUtils.put(getActivity(), ConfigConstant.indecaterNum, 3);
		
		switch (v.getId()) {
		case R.id.iv_home01:
			//第一级的点击事件
			SPUtils.put(getActivity(), "firstNum", 1);
			FragmentTransaction ft = getFragmentManager().beginTransaction();
			GrammarSecondFragment wf = new GrammarSecondFragment();
			Bundle args = new Bundle();
			args.putInt("GRADE_ID", 9001);
			args.putString("GRADE_NAME", "第一级");
			//配置一个跟读的标记
			args.putString("type", "FOLLOWREAD");
			wf.setArguments(args);
			ft.replace(R.id.center_layout, wf);
			ft.addToBackStack(null);
			ft.commit();
			break;
		case R.id.iv_home02:
			//第二级的点击事件
			SPUtils.put(getActivity(), "firstNum", 19);
			FragmentTransaction ft2 = getFragmentManager().beginTransaction();
			GrammarSecondFragment wf2 = new GrammarSecondFragment();
			Bundle args2 = new Bundle();
			args2.putInt("GRADE_ID", 9002);
			args2.putString("GRADE_NAME", "第二级");
			args2.putString("type", "FOLLOWREAD");
			wf2.setArguments(args2);
			ft2.replace(R.id.center_layout, wf2);
			ft2.addToBackStack(null);
			ft2.commit();
			break;
		case R.id.iv_home03:
			//第三级的点击事件
			SPUtils.put(getActivity(), "firstNum", 37);
			FragmentTransaction ft3 = getFragmentManager().beginTransaction();
			GrammarSecondFragment wf3 = new GrammarSecondFragment();
			Bundle args3 = new Bundle();
			args3.putInt("GRADE_ID", 9003);
			args3.putString("type", "FOLLOWREAD");
			args3.putString("GRADE_NAME", "第三级");
			wf3.setArguments(args3);
			ft3.replace(R.id.center_layout, wf3);
			ft3.addToBackStack(null);
			ft3.commit();
			break;
		case R.id.iv_home04:
			//第四级的点击事件
			SPUtils.put(getActivity(), "firstNum", 55);
			FragmentTransaction ft4 = getFragmentManager().beginTransaction();
			GrammarSecondFragment wf4 = new GrammarSecondFragment();
			Bundle args4 = new Bundle();
			args4.putInt("GRADE_ID", 9004);
			args4.putString("type", "FOLLOWREAD");
			args4.putString("GRADE_NAME", "第四级");
			wf4.setArguments(args4);
			ft4.replace(R.id.center_layout, wf4);
			ft4.addToBackStack(null);
			ft4.commit();
			break;
		default:
			break;
		}

	}

	int unitcount1;
	int unitcount2;
	int unitcount3;
	int unitcount4;
	
	private RequestCallBack<String> callBack1 = new RequestCallBack<String>() {

		@Override
		public void onSuccess(ResponseInfo<String> responseInfo) {
			String json = responseInfo.result;
			// 使用Gson把json解析成对象
			// 1 得到gson对象

			Gson gson = new Gson();
			// // 2 使用gson对象的方法
			ClassSecondResp classSecondResp = gson.fromJson(json,
					ClassSecondResp.class);
			unitcount1 = classSecondResp.LIST.size();
		}

		@Override
		public void onFailure(HttpException error, String msg) {

		}
	};
	
	private RequestCallBack<String> callBack2 = new RequestCallBack<String>() {

		@Override
		public void onSuccess(ResponseInfo<String> responseInfo) {
			String json = responseInfo.result;
			// 使用Gson把json解析成对象
			// 1 得到gson对象

			// Log.i("test_class", json);

			Gson gson = new Gson();
			// // 2 使用gson对象的方法
			ClassSecondResp classSecondResp = gson.fromJson(json,
					ClassSecondResp.class);
			unitcount2 = classSecondResp.LIST.size();
		}

		@Override
		public void onFailure(HttpException error, String msg) {

		}
	};

	private RequestCallBack<String> callBack3 = new RequestCallBack<String>() {

		@Override
		public void onSuccess(ResponseInfo<String> responseInfo) {
			String json = responseInfo.result;
			// 使用Gson把json解析成对象
			// 得到gson对象

			Gson gson = new Gson();
			// // 2 使用gson对象的方法
			ClassSecondResp classSecondResp = gson.fromJson(json,
					ClassSecondResp.class);
			unitcount3 = classSecondResp.LIST.size();
		}

		@Override
		public void onFailure(HttpException error, String msg) {
			
		}
	};

	private RequestCallBack<String> callBack4 = new RequestCallBack<String>() {

		@Override
		public void onSuccess(ResponseInfo<String> responseInfo) {
			String json = responseInfo.result;
			// 使用Gson把json解析成对象
			// 1 得到gson对象

			Gson gson = new Gson();
			// 2 使用gson对象的方法
			ClassSecondResp classSecondResp = gson.fromJson(json,
					ClassSecondResp.class);
			unitcount4 = classSecondResp.LIST.size();
		}

		@Override
		public void onFailure(HttpException error, String msg) {

		}
	};

	private RequestCallBack<String> callBack5 = new RequestCallBack<String>() {

		@Override
		public void onSuccess(ResponseInfo<String> responseInfo) {
			String json = responseInfo.result;

			Gson gson = new Gson();
		}

		@Override
		public void onFailure(HttpException error, String msg) {

		}
	};

	@Override
	public void onDestroy() {
		super.onDestroy();
		if (mTimer != null) {

			mTimer.cancel();
			mTimer.purge();
			mTimer = null;
		}
	}
}
